//
//  SIEmoticonPackage.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/10.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import UIKit

/// 表情包
class SIEmoticonPackage: NSObject {

    ///表情包的分组名
    var groupName: String?
    
    /// 背景图片名字
    var bgImageName: String?
    
    /// 表情包目录
    var directory: String? {
        didSet {
            
            guard let directory = directory,
                path = Bundle.main.pathForResource("HMEmoticon.bundle", ofType: nil),
                bundle = Bundle(path: path),
                infoPath = bundle.pathForResource("info.plist", ofType: nil, inDirectory: directory),
                array = NSArray(contentsOfFile: infoPath) as? [[String: String]],
                models = NSArray.yy_modelArray(with: SIEmoticon.self, json: array) as? [SIEmoticon]
                else {
                    return
            }
            
            for m in models {
                
                m.directory = directory
                
            }
            
            emoticons += models
            
        }
        
        
    }
    
    /// 懒加载的表情模型数组
    lazy var emoticons = [SIEmoticon]()
    
    var numberOfPages: Int {
        return (emoticons.count - 1) / 20 + 1
    }
    
    /// 截取表情包中表情数组的子数组
    func emoticon(page: Int) -> [SIEmoticon] {
        
        // 每页的数量
        let count = 20
        let location = page * count
        var length = count
        
        // 判断数组是否越界
        if location + length > emoticons.count {
            length = emoticons.count - location
        }
        
        let range = NSRange(location: location, length: length)
        
        // 截取数组的子数组
        let subArray = (emoticons as NSArray).subarray(with: range)
        
        return subArray as! [SIEmoticon]
    }

    
    override var description: String {
        return yy_modelDescription()
    }
}
